﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using WenSkin.Forms;
using WenSkin.Controls;
using WenSkin;
using static WenSkin.WenConfig;
using System.Text.RegularExpressions;

namespace WenConfigEditCode
{
    public partial class Form1 : WenForm
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void LoadFile()
        {
            if (!Directory.Exists(wenDialogLableTextBox1.Text))
                return;
            string path = wenDialogLableTextBox1.Text;

            flowLayoutPanel1.Controls.Clear();

            foreach (var item in Directory.GetFiles(path, "*.xml"))
            {
                AddItemFile(item, ConfigFormatEnum.Xml);
            }

            foreach (var item in Directory.GetFiles(path, "*.json"))
            {
                AddItemFile(item, ConfigFormatEnum.Json);
            }
        }

        private void AddItemFile(string path, ConfigFormatEnum configFormat)
        {
            FileItemControl fileItem = new FileItemControl
            {
                Path = path,
                Width = flowLayoutPanel1.Width - 20,
                Height = 50,
                ConfigFormat = configFormat,
            };
            fileItem.Click += (s, e) => LoadWenConfig(fileItem);
            flowLayoutPanel1.Controls.Add(fileItem);
        }

        public class FileItemControl : WenButton
        {
            private string path;

            public string Path { get => path; set { path = value; Text = System.IO.Path.GetFileName(value); } }

            public string FolderName => System.IO.Path.GetDirectoryName(path);
            public string FileName => System.IO.Path.GetFileNameWithoutExtension(path);

            public ConfigFormatEnum ConfigFormat { get; set; }
        }

        private void WenButton1_Click(object sender, EventArgs e)
        {
            LoadFile();
        }

        private void LoadWenConfig(FileItemControl fileItem)
        {
            WenConfig config = new WenConfig(fileItem.FolderName, fileItem.FileName, fileItem.ConfigFormat);

            StringBuilder sb = new StringBuilder();
            sb.Append("public static class " + fileItem.FileName + "\r\n");
            sb.Append("{\r\n");

            sb.Append($"\tpublic static WenSkin.WenConfig WenConfig = new WenSkin.WenConfig(System.Windows.Forms.Application.StartupPath,\"{fileItem.FileName}\", WenSkin.WenConfig.ConfigFormatEnum.{fileItem.ConfigFormat});");
            sb.Append($"\r\n");
            AddCode(config.Groups, sb, fileItem);
            sb.Append("}");

            richTextBox1.Text = sb.ToString();
        }


        private void AddCode(WenConfigGroupList groups, StringBuilder sb, FileItemControl fileItem, int level = 0)
        {
            string t = "\t";
            for (int i = 0; i < level; i++)
            {
                t += "\t";
            }

            foreach (var group in groups)
            {
                sb.Append($"{t}public static class {group.Name}\r\n");
                sb.Append(t + "{\r\n");

                foreach (var item in group.Items)
                {
                    string con = $"WenConfig[\"{group.Name}\", \"{item.Name}\"]";

                    sb.Append(t + $"\tpublic static string {item.Name } {{ get=> {con}; set=>{con} =value;}}\r\n");

                    if (item.Items.Count == 2)
                    {
                        sb.Append($"{t}\tpublic static bool {item.Name}B {{get=> {con} ==\"{item.Items[0]}\";set=>{con}=value?\"{item.Items[0]}\":\"{item.Items[1]}\";}}");
                        sb.Append($"\r\n");
                    }

                    if (item.Items.Count > 0 && !item.Items.Exists(q => q.Contains("-") || q.Contains("=")))
                    {
                        sb.Append(t + $"\tpublic static { item.Name }Enum { item.Name }E {{ get=>" +
                            $"({ item.Name }Enum )Enum.Parse(typeof({ item.Name }Enum ), {con}); set=>{con}=value.ToString(); }}\r\n");
                        sb.Append(t + $"\tpublic enum { item.Name }Enum \r\n");
                        sb.Append(t + "\t{\r\n");
                        for (int i = 0; i < item.Items.Count; i++)
                        {
                            sb.Append(t + "\t\t" + item.Items[i] + "=" + i + ",\r\n");
                        }
                        sb.Append(t + "\t}\r\n");
                    }

                }

                if (group.Groups.Count > 0)
                {
                    AddCode(group.Groups, sb, fileItem, level + 1);
                }
                sb.Append(t + "}\r\n");
            }
        }

    }
}
